package org.example.library.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebFilter(value = "/*")
public class Pass implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        resp.setHeader("Access-Control-Allow-Origin", req.getHeader("Origin"));
        resp.setHeader("Access-Control-Allow-Headers," ,
                        "Origin,"+
                        "Access-Control-Request-Headers," +
                        "Access-Control-Allow-Headers," +
                        "DNT," +
                        "X-Requested-With," +
                        "X-Mx-ReqToken," +
                        "Keep-Alive," +
                        "User-Agent," +
                        "X-Requested-With," +
                        "If-Modified-Since," +
                        "Cache-control," +
                        "Content-Type," +
                        "Accept," +
                        "Connection,"+
                        "Cookie," +
                        "X-XSRF-TOKEN,"+
                        "X-CSRF-TOKEN,"+
                        "Authorization");
        resp.setHeader("Access-Control-Allow-Credentials","true");
        resp.setHeader("Access-Control-Allow-Methods","POST,GET,OPTIONS");
        resp.setHeader("Access-Control-Max-Age","86400");
        filterChain.doFilter(servletRequest,servletResponse);
    }
}
